import time

from appium.webdriver.common.appiumby import AppiumBy

# from Util.AppDriver import AppDriver
from Util.BasePage import BasePage


class ZfbLoginPage(BasePage):  # 支付宝未登录状态调用这个方法

    nextStep = (AppiumBy.ID, 'com.alipay.mobile.antui:id/button_text')
    agreeButtom = (AppiumBy.ID, 'com.ali.user.mobile.security.ui:id/btn_privacy_agree')
    moreOptions = (AppiumBy.XPATH, '//android.widget.Button[@text="更多选项"]')
    lastUsed = (AppiumBy.ID, 'com.ali.user.mobile.security.ui:id/tv_verify_tips')
    pwd = (AppiumBy.ID, 'com.ali.user.mobile.security.ui:id/input_edit_view')
    loginBtu = (AppiumBy.ID, 'com.alipay.mobile.antui:id/button_text')
    myslIcon = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("蚂蚁森林")')
    myIcon = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("蚂蚁")')
    mimadenglu = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("密码")')

    def loginPage_mysl(self):
        try:
            # self.findelement(self.nextStep)
            time.sleep(6)
            # self.click(self.myslIcon)  # 如果蚂蚁森林按钮在，说明已登录
            self.findelement(self.myslIcon)  # 如果蚂蚁森林按钮在，说明已登录

            time.sleep(1)

        except:  # 可以打印个日志
            print('蚂蚁森林按钮不在，判断为未登录状态')
            time.sleep(1.5)
            self.driver.back()
            try:
                time.sleep(1.5)
                # self.click(self.nextStep)
                self.click(self.moreOptions)
                time.sleep(1.5)

            except:
                print('更多选项不在')
                try:
                    time.sleep(1.5)
                    self.click(self.nextStep)
                    time.sleep(3.5)

                except:
                    print('第一次尝试登录失败')
                    try:
                        self.driver.back(())
                        time.sleep(1.5)
                        self.click(self.nextStep)
                        time.sleep(3.5)
                    except:
                        print('第二次尝试登录失败，不玩了')
                    else:
                        self.buzhou()
                else:
                    self.buzhou()
            else:
                self.buzhou2()




        else:
            pass
            # self.click(self.agreeButtom)
            # self.buzhou()
        finally:
            pass


    def buzhou(self):
        time.sleep(1.5)
        self.click(self.moreOptions)
        time.sleep(1.5)
        self.click(self.mimadenglu)
        time.sleep(2)
        self.driver.back()  # 收起键盘
        time.sleep(1)
        self.input(self.pwd, 'denglu0302')
        time.sleep(1)
        self.click(self.loginBtu)
        time.sleep(3)

    def buzhou2(self):

        time.sleep(1.5)
        self.click(self.mimadenglu)
        time.sleep(2)
        self.driver.back()  # 收起键盘
        time.sleep(1)
        self.input(self.pwd, 'denglu0302')
        time.sleep(1)
        self.click(self.loginBtu)
        time.sleep(3)


#
# def aaa(self):
#     print('111111111')
#     self.loginPage_mysl('self')
#
#
# if __name__ == '__main__':
#     Zfb = ZfbLoginPage
#     aaa(Zfb)





# try:
#     # 尝试执行的代码
# except ExceptionType1:
#     # 当发生 ExceptionType1 异常时执行的代码
# except ExceptionType2:
#     # 当发生 ExceptionType2 异常时执行的代码
# else:
#     # 如果没有发生异常时执行的代码（可选）
# finally: